Python for AI: A Crash Course

Nov 16 2024 · Python 3.12, JupyterLab 4.2.4

Lesson 04: Working with Local Data (File Operations & Data Handling)

CSV Files Demo

Transcript

00:00In this demo, you’ll read and write CSV files. Just as you did in the demo for text files, you’ll first write some code to write a CSV file and then write code to read it.

00:10Write a new CSV file. Open the working-with-files-starter.ipynb notebook and scroll to the Working with CSV Files section.

00:23Enter the following into a code cell and run it:

import csv

with open('operating-systems.csv', 'w') as file:
  writer = csv.writer(file)

  # Write header row
  writer.writerow(['operating_system', 'creator', 'year'])

  # Write data rows
  data_rows = [
    ["MS-DOS", "Microsoft", 1981],
    ["macOS", "Apple", 1984],
    ["Windows", "Microsoft", 1985],
    ["Linux", "Linus Torvalds", 1991],
  ]
  writer.writerows(data_rows)

00:29The code above opens the file operating-systems.csv for writing. It creates the file if it doesn’t already exist. The code then creates a file object to operate on that file and uses that file object to create a CSV writer object.

00:43Using the writer object, the code writes the first row containing the column header names using the writerow() method. writerow() takes a single list and writes a corresponding single line to the CSV file.

00:54Writing data rows also uses the writerows() method. Here, writerows() takes a list of lists, writing a corresponding line for each inner list to the CSV file.

01:04Open operating-systems.csv with a text editor by right-clicking on the file in the JupyterLab left navigation pane. Then click Open with| Editor. You’ll see this:

operating_system,creator,year
MS-DOS,Microsoft,1981
macOS,Apple,1984
Windows,Microsoft,1985
Linux,Linus Torvalds,1991

01:16If you open operating-systems.csv by double-clicking it in JupyterLab, you’ll see the same data but displayed in a spreadsheet-like table.

01:22Now, write some code to read the CSV file that the previous code wrote. Run the following in a new code cell:

with open("operating-systems.csv", "r") as file:
  reader = csv.reader(file)
  for row in reader:
    print(row)

01:31This code opens operating-systems.csv for reading, creates a file object to operate on that file, and uses that file object to create a CSV reader object.

01:42The reader object is an iterator, so you can use a for loop to read the CSV file line by line. The reader converts each line into a corresponding Python list.

03:03Here’s the output of the code above — one list for each line of the CSV file:

['operating_system', 'creator', 'year']
['MS-DOS', 'Microsoft', '1981']
['macOS', 'Apple', '1984']
['Windows', 'Microsoft', '1985']
['Linux', 'Linus Torvalds', '1991']

01:54Use DictReader to read operating-systems.csv as a collection of dictionaries. This is possible because the file’s first line contains the column headers’ names.

02:05Enter the code below into a new code cell and run it:

with open("operating-systems.csv", "r") as file:
  reader = csv.DictReader(file)
  for row in reader:
    print(row)

02:12This code is similar to the code that reads a CSV file as a collection of lists. The only difference is that instead of passing the file object to csv.reader(), the file object is passed to csv.DictReader().

03:10Here’s the output from the code above:

{'operating_system': 'MS-DOS', 'creator': 'Microsoft', 'year': '1981'}
{'operating_system': 'macOS', 'creator': 'Apple', 'year': '1984'}
{'operating_system': 'Windows', 'creator': 'Microsoft', 'year': '1985'}
{'operating_system': 'Linux', 'creator': 'Linus Torvalds', 'year': '1991'}

02:26Finally, write the CSV version of the “programming languages” file you wrote for the text files demo. This will use DictReader’s counterpart, DictWriter, to write the file and the programming_languages list as the data source.

02:38To use DictWriter, you need to define two data structures:

  1. 02:42A list containing the names of the column headers.
  2. 02:44A list of dictionaries, where each dictionary’s keys are the column headers. In this case, this list is programming_languages.

01:26Run the following in a new code cell:

field_names = [
  "name",
  "creator",
  "year_appeared",
  "site_url",
  "active",
]
with open("programming languages.csv", 'w') as file:
  writer = csv.DictWriter(file, fieldnames=field_names)

  writer.writeheader()
  for row in programming_languages:
    writer.writerow(row)

02:54Note that DictWriter needs more than just the file object; it also requires the list of column header names.

03:01Once the writer object has been created, the writeheader() method is used to write the column header names to the CSV file, and the writerow() method is used to write the data rows.

03:10You can confirm that the code above works by running this in a new code cell:

with open("programming languages.csv", "r") as file:
  reader = csv.DictReader(file)
  for row in reader:
    print(row)

03:15Here’s the first line of output from the above code:

{'name': 'Python', 'creator': 'Guido van Rossum', 'year_appeared': '1991', 'site_url': 'https://www.python.org/', 'active': 'True'}
...